//
//  YYInsuranceVC.m
//  drivingSchool
//
//  Created by 子yu 刘 on 2018/8/9.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "YYInsuranceVC.h"
#import "YYinsuranceCell.h"

@interface YYInsuranceVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,SDCycleScrollViewDelegate>
@property(nonatomic,assign)BOOL isNewCar;
@property(nonatomic,strong)NSArray * linkArr;
@property(nonatomic,strong)NSDictionary * rollDic;
@property (strong, nonatomic) SDCycleScrollView *adCycleScrollView;

@end

@implementation YYInsuranceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bgView.layer.shadowOffset = CGSizeMake(1, 1);
    self.bgView.layer.shadowOpacity = 0.8;
    self.bgView.layer.shadowColor = [UIColor colorWithWhite:0.7 alpha:1].CGColor;
    _isNewCar = YES;
    [self getData];
}

- (void)getData{
    [RequestOperationManager carBanner_success:^(id result) {
        if (DEF_REQ_SUCCESS) {
            self.linkArr = [result[@"data"][@"link"] copy];
            self.rollDic = [result[@"data"][@"roll"] copy];
            if (self.linkArr.count>0) {
                [self.collectionView reloadData];
                self.insuranceHeight.constant = (DEF_SCREEN_WIDTH()-62)/3+85;
                self.insuranceView.hidden = NO;
            }else{
                self.insuranceHeight.constant = 0;
                self.insuranceView.hidden = YES;
            }
            NSMutableArray * imgArr = [NSMutableArray arrayWithCapacity:0];
            [imgArr addObject:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,self.rollDic[@"image"]]];
            
            
            self.adCycleScrollView.imageURLStringsGroup = imgArr;
        }
        
    } failture:^(NSError *error) {
        
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.linkArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    YYinsuranceCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YYinsuranceCell" forIndexPath:indexPath];
    NSString * imgUrl  = [NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,self.linkArr[indexPath.row][@"image"]];
    [cell.icon sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
    return cell;
}

#pragma mark  定义每个UICollectionView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake((DEF_SCREEN_WIDTH()-62)/3,(DEF_SCREEN_WIDTH()-62)/3);
}



#pragma mark  定义每个UICollectionView的横向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

#pragma mark  定义每个UICollectionView的纵向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark  点击CollectionView触发事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

}


- (IBAction)yesClicked:(id)sender {
    [self.btnNo setImage:[UIImage imageNamed:@"sign_inspection vehicle_checkbox_anselected"] forState:UIControlStateNormal];
    [self.btnYes setImage:[UIImage imageNamed:@"sign_inspection vehicle_checkbox"] forState:UIControlStateNormal];
    self.viewHeight.constant = 382;
    self.view4.hidden = NO;
    self.view5.hidden = NO;
    self.lb3.text = @"发动机号";
    self.tf3.placeholder = @"请填写发动机号";
    _isNewCar = YES;
    self.tf3.text = @"";
}
- (IBAction)noClicked:(id)sender {
    [self.btnNo setImage:[UIImage imageNamed:@"sign_inspection vehicle_checkbox"] forState:UIControlStateNormal];
    [self.btnYes setImage:[UIImage imageNamed:@"sign_inspection vehicle_checkbox_anselected"] forState:UIControlStateNormal];
    self.viewHeight.constant = 231;
    self.view4.hidden = YES;
    self.view5.hidden = YES;
    self.lb3.text = @"车牌号";
    self.tf3.placeholder = @"请填写车牌号";
    _isNewCar = NO;
    self.tf3.text = @"";
}

- (IBAction)subscribe:(id)sender {
    if (!DEF_UID) {
        [[AppDelegate shareAppDelegate] enterLogin];
           return;
    }
    if (self.tf2.text.length!=11) {
        [AlertUtil alertMessage:@"请输入正确格式的手机号"];
         return;
    }
    NSDictionary * parm = [NSDictionary dictionary];
    if (_isNewCar) {
        if (self.tf3.text.length<=0) {
            [AlertUtil alertMessage:@"请输入发动机号"];
             return;
        }
        if (self.tf4.text.length<=0) {
            [AlertUtil alertMessage:@"请输入车架号"];
             return;
        }
        if (self.tf5.text.length<=0) {
            [AlertUtil alertMessage:@"请输入车辆型号"];
             return;
        }
        parm = @{@"name":self.tfName.text,
                 @"iphone":self.tf2.text,
                 @"engine":self.tf3.text,
                 @"frame":self.tf4.text,
                 @"type":@"1",
                 @"model":self.tf5.text
                 };
    }else{
        if (self.tf3.text.length<=0) {
            [AlertUtil alertMessage:@"请输入车牌号"];
            return;
        }
        parm = @{@"name":self.tfName.text,
                 @"iphone":self.tf2.text,
                 @"license":self.tf3.text,
                 @"type":@"0",
                 };
     
    }
    
    [AlertUtil showMessage:nil toView:self.view];
    [RequestOperationManager insuranceSubscribe_parm:parm success:^(id result) {
        YYHintVC * hint = (YYHintVC *)[CFUtility getVC:@"YYHintVC" Storyboard:INSURANCE];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (DEF_REQ_SUCCESS) {
            NSDictionary * dict = @{@"image":@"sign_success",
                                    @"title":@"预约成功",
                                    @"content":@"客服稍后会联系您，请保持电话畅通谢谢",
                                    };
            hint.viewDict = dict;
            [self.navigationController pushViewController:hint animated:YES];
        }
    } failture:^(NSError *error) {
 [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    }];
}

- (SDCycleScrollView *)adCycleScrollView{
    if (!_adCycleScrollView) {
        _adCycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:self.bannerView.bounds delegate:self placeholderImage:nil];
        _adCycleScrollView.contentMode = UIViewContentModeScaleAspectFill;
        _adCycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        _adCycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _adCycleScrollView.pageControlDotSize = CGSizeMake(7, 7);
        _adCycleScrollView.tag = 10001;
        [self.bannerView addSubview:_adCycleScrollView];
        [_adCycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
    }
    return _adCycleScrollView;
}


@end
